import 'package:flutter/material.dart';
import 'package:flutter_common_app/api/get_recommend_songs_api.dart';
import 'package:flutter_common_app/data/play_song_data.dart';
import 'package:flutter_common_app/data/song_detail_data.dart';
import 'package:flutter_common_app/pages/recommend_daily_songs/recommend_daily_songs_sliver_header.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/widgets/empty_view/v_empty_view.dart';
import 'package:flutter_common_app/widgets/songs_list_item.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RecommendDailySongsPage extends StatefulWidget {
  @override
  _RecommendDailySongsPageState createState() =>
      _RecommendDailySongsPageState();
}

class _RecommendDailySongsPageState extends State<RecommendDailySongsPage> {
  List<SongDetailData> songsList = [];

  @override
  void initState() {
    GetRecommendSongsApi(context).send().then((res) {
      setState(() {
        songsList = SongDetailData.fromJsonList(res.data['data']['dailySongs']);
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverPersistentHeader(
            delegate: RecommendDailySongsSliverHeader(songsList),
            pinned: true,

          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                SongDetailData data = songsList[index];
                return SongsListItem(
                  name: data.name,
                  describe: '${data.ar.map((item) => item.name).toList().join('/')}' +'-'+data.al.name,
                  index: index,
                  picUrl: data.al.picUrl,
                  clickPlayCallBack: () {
                    Provider.of<AudioModel>(context, listen: false).playOneSong(
                      PlaySongData(
                        data.id,
                        name: data.name,
                        artists: '${data.ar.map((item) => item.name).toList().join('/')}' +'-'+data.al.name,
                        picUrl: data.al.picUrl + '?param=50y50',
                      ),
                    );
                  },
                );
              },
              childCount: songsList.length,
            ),
          ),
          SliverToBoxAdapter(
            child: VEmptyView(100.w),
          ),
        ],
      ),
    );
  }
}
